package com.gksc.video.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gksc.video.data.JsonResponse;
import com.gksc.video.utils.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: WebInterceptor
 * @description: 主拦截器入口
 * @author: Liao.yx
 * @email: 1229053515@qq.com
 * @create: 2020年07月28日 16时15分
 * @copyRight: 2020 liaoyongxiang All rights reserved.
 **/
@Component
@Slf4j
public class WebInterceptor implements HandlerInterceptor {
    @Autowired
    private GkzfWebProperties webProperties;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String url = request.getRequestURI();
        String realIp = Tool.getRealIpAddress(request);
        if (webProperties.isRequestInterceptEnable()) {
            for (WhiteIpDefinition rule : webProperties.getWhiteRules()) {
                if (url.equals(rule.getUri())) {
                    List<String> ipList = rule.getIps();
                    if (ipList != null || ipList.size() > 0) {
                        List<String> ipVlan = getIpVlan(ipList);
                        for (String ip : ipVlan) {
                            if (ip.startsWith(realIp)) {
                                return true;
                            }
                        }
                        ipList.removeAll(ipVlan);
                        for (String ip : ipList) {
                            if (realIp.equals(ip)) {
                                return true;
                            }
                        }
                        responseEx(response, "此客户端不允许访问该请求");
                        return false;
                    } else {
                        responseEx(response, "系统配置有误");
                        return false;
                    }
                }
            }
            return true;
        } else {
            return true;
        }
    }

    /**
     * 获取网段的IP地址
     *
     * @param ipList ip列表
     * @return
     */
    private List<String> getIpVlan(List<String> ipList) {
        List<String> result = new ArrayList<>();
        for (String ip : ipList) {
            if (ip.indexOf("*") > 0) {
                result.add(ip);
            }
        }
        return result;
    }

    /**
     * 输出异常
     *
     * @param response 请求对象
     * @param errorMsg 错误消息
     */
    private void responseEx(HttpServletResponse response, String errorMsg) {
        try {
            String json = new ObjectMapper().writeValueAsString(JsonResponse.error(errorMsg));
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            response.getWriter().append(json);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
